/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.hiddendanger.controller;

import com.koron.common.Excel;
import com.koron.common.StaffAttribute;
import com.koron.common.bean.StaffBean;
import com.koron.common.permission.SPIAccountAnno;
import com.koron.hiddendanger.bean.SafetyObservBean;
import com.koron.hiddendanger.service.api.SafetyObservService;
import com.koron.hiddendanger.uitls.BusinessCode;
import com.koron.system.annotation.Log;
import com.koron.system.discrete.BusinessManipulation;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.swan.bean.MessageBean;
import springfox.documentation.annotations.ApiIgnore;

import java.io.File;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "safetyObserv", description = "安全观察")
@RestController
@RequestMapping("/safetyObserv")
public class SafetyObservController {

	@Autowired
	private SafetyObservService service;
	private final static Logger logger = LoggerFactory.getLogger(SafetyObservController.class);

	@ApiOperation("删除")
	@ApiImplicitParam(name = "id", value = "id", paramType = "query", dataType = "String")
    @RequestMapping(value = "/delete.htm", method = { RequestMethod.POST })
	@Log(title = "删除安全观察记录", businessModule = "安全观察", businessManipulation = BusinessManipulation.DELETE)
	public MessageBean<?> delete(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,String id){
		if (null==user){
			MessageBean<?> msg=new MessageBean<>();
			msg.setCode(BusinessCode.NOT_LONGIN.getCode());
			msg.setDescription(BusinessCode.NOT_LONGIN.getDescription());
			return msg;
		}
		return service.delete(id,user);
	}

	@ApiOperation("详情")
	@ApiImplicitParam(name = "id", value = "id", paramType = "query", dataType = "String")
    @RequestMapping(value = "/details.htm", method = { RequestMethod.GET })
	public MessageBean<?> details(String id){
		return service.details(id);
	}

	@ApiOperation("添加安全观察记录")
    @RequestMapping(value = "/insert.htm", method = { RequestMethod.POST })
    @Log(title = "添加安全观察记录", businessModule = "安全观察", businessManipulation = BusinessManipulation.INSERT)
    public MessageBean<?> insert(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,@RequestBody SafetyObservBean bean){
		if (null==user){
			MessageBean<?> msg=new MessageBean<>();
			msg.setCode(BusinessCode.NOT_LONGIN.getCode());
			msg.setDescription(BusinessCode.NOT_LONGIN.getDescription());
			return msg;
		}
		return service.insert(bean, user);
	}

	@ApiOperation("修改")
    @RequestMapping(value = "/update.htm", method = { RequestMethod.POST })
    @Log(title = "修改安全观察记录", businessModule = "安全观察", businessManipulation = BusinessManipulation.UPDATE)
    public MessageBean<?> update(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,@RequestBody SafetyObservBean bean){
		if (null==user){
			MessageBean<?> msg=new MessageBean<>();
			msg.setCode(BusinessCode.NOT_LONGIN.getCode());
			msg.setDescription(BusinessCode.NOT_LONGIN.getDescription());
			return msg;
		}
		return service.update(bean, user);
	}

	@ApiOperation("查询")
	@ApiImplicitParams({
    	@ApiImplicitParam(name = "observerCode", value = "观察人code",paramType = "query", dataType = "String"),
    	@ApiImplicitParam(name = "observerName", value = "观察人name",paramType = "query", dataType = "String"),
    	@ApiImplicitParam(name = "corpCode", value = "公司code",paramType = "query", dataType = "String"),
    	@ApiImplicitParam(name = "behaviorTypeId", value = "操作类型id",paramType = "query", dataType = "String"),
    	@ApiImplicitParam(name = "gridCode", value = "观察网格code",paramType = "query", dataType = "String"),
    	@ApiImplicitParam(name = "deptCode", value = "观察人部门",paramType = "query", dataType = "String"),
    	@ApiImplicitParam(name = "biginDate", value = "开始日期",paramType = "query", dataType = "String"),
    	@ApiImplicitParam(name = "endDate", value = "结束日期",paramType = "query", dataType = "String"),
        @ApiImplicitParam(name = "page", value = "当前页", paramType = "query", dataType = "Integer"),
        @ApiImplicitParam(name = "pageSize", value = "每页条数",paramType = "query", dataType = "Integer")})
    @RequestMapping(value = "/query.htm", method = { RequestMethod.GET })
	public MessageBean<?> query(String observerCode,String observerName,String corpCode,String behaviorTypeId,
			String gridCode,String deptCode,Date biginDate,	Date endDate,Integer page,Integer pageSize){
		return service.list(observerCode, observerName, corpCode, behaviorTypeId, gridCode,deptCode, biginDate, endDate, page, pageSize);
	}


	@ApiOperation("安全观察列表导出接口")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "observerCode", value = "观察人code",paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = "observerName", value = "观察人name",paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = "corpCode", value = "公司code",paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = "behaviorTypeId", value = "操作类型id",paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = "gridCode", value = "观察网格code",paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = "deptCode", value = "观察人部门",paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = "biginDate", value = "开始日期",paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = "endDate", value = "结束日期",paramType = "query", dataType = "String"),
	})
	@RequestMapping(value = "/exportList.htm", method = RequestMethod.GET)
	public HttpEntity<?> exportList(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,String observerCode,String observerName,String corpCode,String behaviorTypeId,
									String gridCode,String deptCode,Date biginDate,	Date endDate) {
		if (null == user) {
			return null;
		}
		List<SafetyObservBean> list = service.exportList(user, corpCode, observerCode,observerName,gridCode,behaviorTypeId,deptCode,biginDate,endDate);
		Map<String, Object> map = new HashMap<>();
		map.put("data", list);
		File targetFile = new File("static/excel/safetyObservList.xlsx");
		try {
			InputStream in = getClass().getClassLoader().getResourceAsStream("static/excel/safetyObservList.xlsx");
			FileUtils.copyInputStreamToFile(in, targetFile);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return Excel.createFile("安全观察列表", targetFile, "safetyObservList.btl", map);
	}
}
